/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.tkorg.coauthorgraph.servlet.graph;

import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.tkorg.coauthorgraph.bo.CoAuthorGraphBO;
import uit.tkorg.coauthorgraph.dto.ConfProgCommitteeGraphDTO;
import uit.tkorg.coauthorgraph.dto.ConferenceDTO;

/**
 *
 * @author Tin Huynh
 */
public class BuildProgramCommitteeGraphServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            ArrayList confCommitteeGraphDTOList = new ArrayList();
            HttpSession session = request.getSession();
            String depthLevel = request.getParameter("depthLevel");
            ArrayList confDTOList = (ArrayList) session.getAttribute("confDTOList");
            CoAuthorGraphBO coAuthorGraphBO = new CoAuthorGraphBO();
            if (confDTOList != null && confDTOList.size() > 0) {
                for (int i=0; i<confDTOList.size(); i++) {
                    ConferenceDTO confDTO = (ConferenceDTO) confDTOList.get(i);
                    ArrayList authorListOfProgCommittee = confDTO.getAuthorListOfProgCommittee();
                    UndirectedSparseGraph graph = coAuthorGraphBO.buildCoAuthorGraph(authorListOfProgCommittee, depthLevel);
                    ConfProgCommitteeGraphDTO confCommitteeGraphDTO = new ConfProgCommitteeGraphDTO();
                    confCommitteeGraphDTO.setConferenceName(confDTO.getConferenceName());
                    confCommitteeGraphDTO.setGraph(graph);
                    confCommitteeGraphDTOList.add(confCommitteeGraphDTO);
                }

                session.setAttribute("confCommitteeGraphDTOList", confCommitteeGraphDTOList);
            }

            String resultMessage = "Co-Author Graph of the conference's program committee is built" + " ... confCommitteeGraphDTOList:" + confCommitteeGraphDTOList.size();
            request.setAttribute("resultMessage", resultMessage);
            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/MetricsCalculation.jsp");
            dispatcher.forward(request, response);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
